package com.lc.user.express.my;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.user.express.BaseSecondActivity;
import com.lc.user.express.R;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.app.AppManager;
import com.lc.user.express.httpserver.GetBankCardAdd;
import com.lc.user.express.httpserver.GetPayPwd;
import com.lc.user.express.httpserver.GetUpdatePayPwd;
import com.lc.user.express.utils.GetToast;
import com.lc.user.express.utils.image.MD5;
import com.lc.user.express.view.PayKeyboardPopupWindow;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;



/**
 * Created by Administrator on 2016/3/15. 确认支付密码
 */
public class SurePayPasswordActivity extends BaseSecondActivity {
    public static final int TYPE_SET = 0x99;//设置支付密码
    public static final int TYPE_RESET = 0x98;//重置支付密码
    public static final int TYPE_FORGET = 0x97;//忘记支付密码
    private LinearLayout ll_container;
    private ArrayList<String> mList = new ArrayList<String>();
    private TextView tv_disc;
    private String pwd = "";
    private PayKeyboardPopupWindow popup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sure_pay_password);
        initView();
    }

    private void initView() {
        setTitle("确认支付密码");
        ((TextView) findViewById(R.id.tv_public)).setOnClickListener(this);
        ll_container = (LinearLayout) findViewById(R.id.ll_container);
        ll_container.setOnClickListener(this);
        tv_disc = (TextView) findViewById(R.id.tv_dic);
        tv_disc.setText("请再次输入确认");
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_container:
                showPasswordPopupWindow();
                break;
            case R.id.tv_public:
                if (pwd.equals(getIntent().getStringExtra("pwd"))) {
                    if (getIntent().getFlags() == TYPE_SET) {
                        addBankCard();
                    } else if (getIntent().getFlags() == TYPE_RESET) {
                        updatePayPwd();
                    } else if (getIntent().getFlags() == TYPE_FORGET) {
                        setPayPwd();
                    }

                } else {
                    GetToast.useString(cnt, "两次输入密码不一致");
                }

                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (popup != null) popup.dismiss();
    }

    private void showPasswordPopupWindow() {
        popup = new PayKeyboardPopupWindow(this);
        popup.showAtLocation(findViewById(R.id.ll_root), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        popup.setOnInputFinishListener(new PayKeyboardPopupWindow.OnInputFinish() {
            @Override
            public void onInput(String password) {
                if (mList.size() <= 0) {
                    addTextView(SurePayPasswordActivity.this, 1, password);
                    mList.add(password);
                } else if (mList.size() == 5) {
                    addTextView(SurePayPasswordActivity.this, 1, password);
                    mList.add(password);
                    StringBuffer sb = new StringBuffer();
                    for (String item : mList) {
                        sb.append(item);
                    }
                    pwd = sb.toString();

                    if (!pwd.equals(getIntent().getStringExtra("pwd"))) {
                        GetToast.useString(cnt, "两次输入密码不一致");
                    } else {
                        popup.dismiss();
                    }
                } else if (mList.size() < 6) {
                    addTextView(SurePayPasswordActivity.this, 0, password);
                    mList.add(password);
                }
            }

            @Override
            public void onDelete() {
                if (mList.size() > 0) {
                    ll_container.removeViewAt(mList.size() - 1);
                    mList.remove(mList.get(mList.size() - 1));
                }
            }
        });


    }

    private void addTextView(Activity context, int type, String s) {
        LinearLayout ll = (LinearLayout) View.inflate(context, R.layout.item_key, null);
        if (type == 1) {
            ll.setLayoutParams(new LinearLayout.LayoutParams(AppContext.ScaleScreenHelper.getWidthHeight(116),// 两侧的view稍宽
                    LinearLayout.LayoutParams.WRAP_CONTENT));
        } else {
            ll.setLayoutParams(new LinearLayout.LayoutParams(AppContext.ScaleScreenHelper.getWidthHeight(110),
                    LinearLayout.LayoutParams.WRAP_CONTENT));
        }

        AppContext.ScaleScreenHelper.loadView(ll);

        TextView textView = (TextView) ll.findViewById(R.id.tv_key);
        textView.setText(s);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPasswordPopupWindow();
            }
        });

        ll_container.addView(ll);
    }

    private void setPayPwd() {
        new GetPayPwd(AppContext.myPreferences.getUid(), MD5.Md5(MD5.Md5(pwd)), new AsyCallBack<Object>() {
            @Override
            public void onSuccess(String toast, int type, Object o) throws Exception {
                finish();
                AppManager.getAppManager().finishActivity(SetPayPasswordActivity.class);
                AppManager.getAppManager().finishActivity(LoginActivity.class);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                if (!TextUtils.isEmpty(toast)) {
                    GetToast.useString(cnt, toast);
                }
            }
        }).execute(cnt);
    }

    private void updatePayPwd() {
        new GetUpdatePayPwd(AppContext.myPreferences.getUid(), MD5.Md5(MD5.Md5(getIntent().getStringExtra("oldpwd"))), MD5.Md5(MD5.Md5(pwd)), new AsyCallBack<Object>() {
            @Override
            public void onSuccess(String toast, int type, Object o) throws Exception {
                finish();
                AppManager.getAppManager().finishActivity(SetPayPasswordActivity.class);
                AppManager.getAppManager().finishActivity(ChangePayPasswordActivity.class);
                AppManager.getAppManager().finishActivity(AddBankCardActivity.class);
                AppManager.getAppManager().finishActivity(LoginActivity.class);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                if (!TextUtils.isEmpty(toast)) {
                    GetToast.useString(cnt, toast);
                }
            }
        }).execute(cnt);
    }

    private void addBankCard() {
        GetBankCardAdd getBankCardAdd = new GetBankCardAdd(AppContext.myPreferences.getUid(), "", "", "", "", "", new AsyCallBack<Object>() {
            @Override
            public void onSuccess(String toast, int type, Object o) throws Exception {
                if ("1".equals(o)) {
                    if (RechargeActivity.onRefresh != null) {
                        RechargeActivity.onRefresh.onRefresh();
                    }
                    finish();
                    AppManager.getAppManager().finishActivity(SetPayPasswordActivity.class);
                    AppManager.getAppManager().finishActivity(ChangePayPasswordActivity.class);
                    AppManager.getAppManager().finishActivity(AddBankCardActivity.class);
                    AppManager.getAppManager().finishActivity(LoginActivity.class);
                }
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                if (!TextUtils.isEmpty(toast)) {
                    GetToast.useString(cnt, toast);
                }
            }
        });
        getBankCardAdd.userid = AppContext.myPreferences.getUid();
        getBankCardAdd.mobile = getIntent().getStringExtra("mobile");
        getBankCardAdd.banktype = getIntent().getStringExtra("bank");
        getBankCardAdd.name = getIntent().getStringExtra("name");
        getBankCardAdd.bankcard = getIntent().getStringExtra("bankcard_num");
        getBankCardAdd.paypwd = MD5.Md5(MD5.Md5(pwd));
        getBankCardAdd.execute(cnt);

    }
}

